/* 
 * File:   ipd_smoothing_spatial_filter.cpp
 * Author: dima
 * 
 * Created on 25 января 2015 г., 14:44
 */
#include "ipd_smoothing_spatial_filter.h"
#include "ipd_exception.h"
namespace ipd
{

ipd_smoothing_spatial_filter::ipd_smoothing_spatial_filter(ipd_img &img) : ipd_filter(img)
{
    maska=NULL;
}

ipd_smoothing_spatial_filter::~ipd_smoothing_spatial_filter()
{
    delete_maska();
}

void ipd_smoothing_spatial_filter::init(int sz, tp_smoothing_spatial_filter tp_filter)//object initialization
{
    size = sz;
    switch (tp_filter)
    {
    case weighted_average:
    {
        create_weighted_average_mask();
        break;
    }
    case averaging_filter:
    default:
    {
        create_averaging_filter_mask();
        break;
    }
    }
    ipd_filter::init(size, size, maska);
}

void ipd_smoothing_spatial_filter::create_maska()// create maska
{
    delete_maska();
    maska = new int [size * size];
}

void ipd_smoothing_spatial_filter::delete_maska()// delete maska
{
    if (maska != NULL)
        delete []maska;
    maska = NULL;
}

void ipd_smoothing_spatial_filter::create_weighted_average_mask()
{
    create_maska();
    int shift = (size - 1) / 2;
    int index, index_x, index_y;
    for (int i = 0; i < size; i++)
    {
        index_y = i > shift ? size - 1 - i : i;
        for (int j = 0; j < size; j++)
        {
            index_x = j > shift ? size - 1 - j : j;
            index = index_x + index_y - shift;
            if (index < 0)
                maska[i * size + j] = 1;
            else
                maska[i * size + j] = 1 << (index + 1); //pow(2, index + 1);
        }
    }
}

void ipd_smoothing_spatial_filter::create_averaging_filter_mask()//create averaging filter mask
{
    create_maska();
    int sz = size*size;
    for (int i = 0; i < sz; i++)//averaging_filter
        maska[i] = 1;
}

ipd_smoothing_spatial_filter::ipd_smoothing_spatial_filter(const ipd_smoothing_spatial_filter& orig) : ipd_filter(orig)
{
}
}